import * as shelfService from '../../services/shelfDown';
import {queryAssetType} from '../../services/assetType';
import {qryAllUser} from '../../services/user';
import {queryAssetBrand,queryAssetModelAll} from '../../services/assetModel';
import {queryRoomList} from '../../services/tRoom';
import {querAssetListByParam} from '../../services/equipment';

export default {
  namespace: 'shelfDown',
  state: {
    list: [],
    assetTypeTree:[],
    deptTree:[],
    roomList:[],
    assetBrandList:[],
    assetModelList:[],
    userList:[],
    assetList:[],
    current : 1,
    currentSize : 10,
    total : 0, 
    modalVisible : false,
    modalType : 'create',
    newKey : '',
    detailList:[],
    currentItem: {},
    viewModalVisible:false,
    viewItem:{},
    viewDetailList:[],
    pullOffId:"",
    detailModalVisible:false,
    secondModalVisible:false,
    showDetailModalVisible:false,
    assetCurrent:1,
    assetCurrentSize : 10,
    assetTotal : 0, 
    assetItem:{},
    selectedRows:[],
  },
  reducers: {
    updateState (state, { payload }) {
      return { ...state, ...payload}
    },
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryListByParamSuc(state, { payload }) {
      return { ...state, ...payload };
    },    
    changeDetailList(state, { payload }) {
      return { ...state, ...payload, secondModalVisible: false };
    },   
    queryBrandListSuc(state, { payload: assetBrandList }) {      
      return { ...state, assetBrandList };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
    queryUserSuc(state, { payload:userList}) {
      return { ...state, userList };
    },
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    getJoberNumSuc(state, { payload}) {
      return { ...state, pullOffId:payload };
    },
    
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, detailList:[], newKey : new Date().getTime() + '' }
    },

    showViewModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , viewModalVisible: true }
    },
    hideViewModal (state) {
      return { ...state, viewModalVisible: false,newKey : new Date().getTime() + '' }
    },
    showCreateDetailModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , detailModalVisible: true }
    },
    hideCreateDetailModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , detailModalVisible: false }
    },
    showSecondModal (state, { payload }) {
        return { ...state, ...payload,  secondModalVisible: true }
    },
    hideSecondModal (state, { payload }) {
      return { ...state, ...payload, secondModalVisible: false }
    },
    upState (state, { payload }) {
      return { ...state, ...payload}
    },
    
  },
  effects: {      
    *queryUser({ payload },{ call, put }) {    
      const { body }  = yield call(qryAllUser);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    *queryAssetBrand({ payload },{ call, put }) {
      const { body } = yield call(queryAssetBrand,payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    }, 
    *queryAssetModel({ payload },{ call, put }) {
      const { body } = yield call(queryAssetModelAll,payload);
      yield put({ type: 'updateState', payload: {assetModelList:body.data} });
    },
    *queryAssetList({ payload }, { call, put }) {
      yield put({ type: 'querAssetListByParam', payload: { roomId: payload.roomId,searchStatusType:3} });
      yield put({ type: 'showSecondModal', payload: { assetItem: payload} });
    },
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(shelfService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *qryById({ payload },{ call, put }){
        const { body } = yield call(shelfService.getJoberNum,payload);
        yield put({ type: 'getJoberNumSuc', payload: body.data, });
        yield put({type: 'showModal'});
        yield put({type: 'queryAssetType'});
        yield put({ type: 'queryAssetBrand', payload: {  },});
    }, 

    *onViewItem({ payload },{ call, put }){
      const { body } = yield call(shelfService.qryById,payload);
      yield put({type: 'showViewModal', payload: {viewItem: body.data, viewDetailList:body.data.detailList},
      });
    },
    
    *showPutOffDetail({ payload },{ call, put }){
      const { body } = yield call(shelfService.qryById,payload);
      yield put({type: 'upState', payload: {viewItem: body.data, viewDetailList:body.data.detailList,showDetailModalVisible:true},
      });
    }, 
    *querAssetListByParam({ payload },{ call, put }){
      const { body } = yield call(querAssetListByParam,payload);
      yield put({ type: 'queryListByParamSuc', payload: { assetList:body.data,assetCurrent:body.current, assetCurrentSize:body.size, assetTotal:body.iTotalRecords}});
    },
    *addPullOff({ payload },{ call, put }){
      const { body } = yield call(shelfService.create,payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload' });
    },

    *doPullOff({ payload },{ call, put }){
      const { body } = yield call(shelfService.doPullOff,payload);
      yield put({type: 'upState', payload: {showDetailModalVisible:false},});
      yield put({ type: 'reload' });
    },

    *edit({ payload },{ call, put }){
      const { body } = yield call(shelfService.edit,payload);
      yield put({ type: 'reload' });
    },
    *remove({ payload },{ call, put }){
      const { body } = yield call(shelfService.remove,payload.id);
      yield put({ type: 'queryList', payload:payload.search });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryList', payload:{ 'name':'' }});
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/shelf/down') {
          dispatch({ type: 'queryUser' });
          dispatch({ type: 'queryRoom' });          
          dispatch({ type: 'queryList', payload:{ 'status':'' }});
        }
      });
    },
  },
};
